package cn.ffcs.uec.outStaff.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffRetireEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecRetire;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecOutStaffRetireEditComposer extends BasePortletComposer {
	private Logger log = Logger.getLogger(this.getClass());
	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffRetireEditBean bean = new UecOutStaffRetireEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecStaffInfo;
	/**
	 * 
	 */
	private UecRetire uecRetire;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 */
	public void onCreate$uecRetireEditWindow() {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 政治面貌及党籍情况 partyName
		List<NodeVo> nodeVoGlbdef10List = new ArrayList<NodeVo>();
		String[][] glbdef10 = { { "Y", "是" },
				{ "N", "否" } };
		for (int i = 0; i < glbdef10.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef10[i][j]);
				nodeVo.setName(glbdef10[i][j + 1]);
			}
			nodeVoGlbdef10List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef10(), nodeVoGlbdef10List);
	}

	/**
	 * bindBean
	 */
	public void bindBean() {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecRetireEditWindow().setTitle("新增离退情况");
			bean.getUecRetireEditDiv().setHeight("30%");

		}
		if ("view".equals(opType)) {
			this.bean.getUecRetireEditWindow().setTitle("查看离退情况");
			uecRetire = (UecRetire) arg.get("uecRetire");
			if (null != uecRetire) {
				PubUtil.fillBeanFromPo(uecRetire, bean);
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecRetireEditDiv(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecRetireEditWindow().setTitle("编辑离退情况");
			bean.getUecRetireEditDiv().setHeight("30%");
			uecRetire = (UecRetire) arg.get("uecRetire");
			if (null != uecRetire) {
				PubUtil.fillBeanFromPo(uecRetire, bean);
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			uecRetire = UecRetire.newInstance();
			log.warn("-----------------" + bean.getOtherfee().getValue());
			PubUtil.fillPoFromBean(bean, uecRetire);
			dateTransToString();
			uecRetire.setStaffId(uecStaffInfo.getOutStaffId());
			uecRetire.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecRetire.setPkPsndoc(uecStaffInfo.getPkPsnMsg());
			uecRetire.setDr(1L);
			// 创建人
			uecRetire.setCreator(currentName);
			// 创建时间
			uecRetire.setCreationtime(DateUtil.getCurrenDate());
			uecRetire.addOnly();
			Events.postEvent("onOK", this.self, uecRetire);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecRetire);
			dateTransToString();
			// 修改人
			uecRetire.setModifier(currentName);
			// 修改时间
			uecRetire.setModifiedtime(DateUtil.getCurrenDate());
			uecRetire.updateOnly();
			Events.postEvent("onOK", this.self, uecRetire);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecRetireEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		/*
		 * if (bean.getLastflag().getValue().length() > 1) { return
		 * "最近记录标识只能一个字符长度！";
		 */
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		// }
		return null;
	}

	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecRetire.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecRetire
					.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef2().getValue())) {
			uecRetire
					.setGlbdef2(formatter.format(bean.getGlbdef2().getValue()));
		}
	}

}
